<?php
/* This file is part of Mirasol CMS
   (C) 2011 by Chris Alban Hansen.
   Released under the terms of the GNU General Public License.
   See COPYING in the top level directory of the Mirasol CMS installation. */

include "{$_SERVER['DOCUMENT_ROOT']}/includes/config.php";
include "{$_SERVER['DOCUMENT_ROOT']}/includes/core.php";
include "{$_SERVER['DOCUMENT_ROOT']}/includes/db.php";
include "{$_SERVER['DOCUMENT_ROOT']}/includes/samples.php";

if (isset ($install))
{
  $connection = db_open ();
  
  /* Create 'users' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_users)." LIMIT 0,1");
  if (!$check)
    mysql_query ("CREATE TABLE ".db_maketablename ($table_users)." (id INT NOT NULL auto_increment, fullname VARCHAR (50), username VARCHAR (20), email VARCHAR (50), passwd VARCHAR (35), salt INT, rights INT, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
  
  /* See if there is an 'admin' user */
  $result = mysql_query ("SELECT id FROM ".db_maketablename ($table_users)." WHERE username LIKE 'admin'");
  $createadmin = mysql_num_rows ($result) == 0 ? 1 : 0;
  mysql_free_result ($result);
  
  /* Create 'settings' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_settings)." LIMIT 0,1");
  if (!$check)
    mysql_query ("CREATE TABLE ".db_maketablename ($table_settings)." (id INT NOT NULL auto_increment, sitename VARCHAR (100), domainname VARCHAR (255), protocol VARCHAR (20), PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
  
  /* Upgrade 'settings' from version 0.5 */
  $check = mysql_query ("SELECT protocol FROM ".db_maketablename ($table_settings)." LIMIT 0,1");
  if (!$check)
    mysql_query ("ALTER TABLE ".db_maketablename ($table_settings)." ADD COLUMN protocol VARCHAR (20)");

  /* See if there is a readable row in 'settings' */
  $result = mysql_query ("SELECT id FROM ".db_maketablename ($table_settings)." LIMIT 1");
  if (mysql_num_rows ($result) < 1)
    mysql_query ("INSERT INTO ".db_maketablename ($table_settings)." (sitename, domainname, protocol) VALUES ('A ".mysql_real_escape_string ($app_name)." powered website', 'mydomain.com', 'http')");
  mysql_free_result ($result);
  
  /* Create 'templates' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_templates)." LIMIT 0,1");
  if (!$check)
    {
      mysql_query ("CREATE TABLE ".db_maketablename ($table_templates)." (id INT NOT NULL auto_increment, name VARCHAR(50), fields TEXT, xslt LONGTEXT, php LONGTEXT, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
      mysql_query ("INSERT INTO ".db_maketablename ($table_templates)." (name, fields, xslt, php) VALUES ('Frontpage', '".mysql_real_escape_string ($sample_fields1)."', '".mysql_real_escape_string ($sample_layout1)."', '')");
      $templ_id1 = mysql_insert_id ($connection);
      mysql_query ("INSERT INTO ".db_maketablename ($table_templates)." (name, fields, xslt, php) VALUES ('About', '".mysql_real_escape_string ($sample_fields2)."', '".mysql_real_escape_string ($sample_layout2)."', '')");
      $templ_id2 = mysql_insert_id ($connection);
    }
  
  /* Create 'sublayouts' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_sublayouts)." LIMIT 0,1");
  if (!$check)
    {
      mysql_query ("CREATE TABLE ".db_maketablename ($table_sublayouts)." (id INT NOT NULL auto_increment, name VARCHAR(50), xslt LONGTEXT, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
      mysql_query ("INSERT INTO ".db_maketablename ($table_sublayouts)." (name, xslt) VALUES ('Poweredby', '".mysql_real_escape_string ($sample_sublayout)."')");
    }
  
  /* Create 'pages' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_pages)." LIMIT 0,1");
  if (!$check)
    {
      mysql_query ("CREATE TABLE ".db_maketablename ($table_pages)." (id INT NOT NULL auto_increment, path VARCHAR (255), version INT, starttime INT, endtime INT, template INT, xmlfeed LONGTEXT, guid VARCHAR (40), lastedit INT, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
      $now = time ();
      mysql_query ("INSERT INTO ".db_maketablename ($table_pages)." (path, version, starttime, endtime, template, xmlfeed, lastedit) VALUES ('[root]', '1', '".time ()."', '0', '$templ_id1', '".mysql_real_escape_string ($sample_page1)."', '$now')");
      $now++;
      mysql_query ("INSERT INTO ".db_maketablename ($table_pages)." (path, version, starttime, endtime, template, xmlfeed, lastedit) VALUES ('About', '1', '".time ()."', '0', '$templ_id2', '".mysql_real_escape_string ($sample_page2)."', '$now')");
    }
  
  /* Upgrade 'pages' from version 0.6 */
  $check = mysql_query ("SELECT guid FROM ".db_maketablename ($table_pages)." LIMIT 0,1");
  if (!$check)
    mysql_query ("ALTER TABLE ".db_maketablename ($table_pages)." ADD COLUMN guid VARCHAR (40)");
  $check = mysql_query ("SELECT lastedit FROM ".db_maketablename ($table_pages)." LIMIT 0,1");
  if (!$check)
    mysql_query ("ALTER TABLE ".db_maketablename ($table_pages)." ADD COLUMN lastedit INT");

  /* Create 'pages_review' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_pages_review)." LIMIT 0,1");
  if (!$check)
    mysql_query ("CREATE TABLE ".db_maketablename ($table_pages_review)." (id INT NOT NULL auto_increment, path VARCHAR (255), version INT, starttime INT, endtime INT, template INT, xmlfeed LONGTEXT, guid VARCHAR (40), lastedit INT, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
  
  /* Upgrade 'pages_review' from version 0.6 */
  $result = mysql_query ("SELECT * FROM ".db_maketablename ($table_pages)." WHERE guid IS NULL OR guid=''");
  if (mysql_num_rows ($result) > 0)
    {
      $now = time ();
      while ($row = mysql_fetch_array ($result))
        {
          $guid = createguid ();
          mysql_query ("UPDATE ".db_maketablename ($table_pages)." SET guid='$guid', lastedit='$now' WHERE id='{$row['id']}'");
          if (!mysql_query ("INSERT INTO ".db_maketablename ($table_pages_review)." (path, version, starttime, endtime, template, xmlfeed, guid, lastedit) VALUES ('{$row['path']}', '{$row['version']}', '{$row['starttime']}', '{$row['endtime']}', '{$row['template']}', '".mysql_real_escape_string ($row['xmlfeed'])."', '$guid', '$now')"))
            die (mysql_error ($connection));
          $now++;
        }
    }
  mysql_free_result ($result);
  
  /* Create 'styles' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_styles)." LIMIT 0,1");
  if (!$check)
  {
    mysql_query ("CREATE TABLE ".db_maketablename ($table_styles)." (id INT NOT NULL auto_increment, name VARCHAR (50), css LONGTEXT, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
    mysql_query ("INSERT INTO ".db_maketablename ($table_styles)." (name, css) VALUES ('default', '".mysql_real_escape_string ($sample_style)."')");
    $style_id = mysql_insert_id ($connection);
  }
  
  /* Create 'styles_rel' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_styles_rel)." LIMIT 0,1");
  if (!$check)
  {
    mysql_query ("CREATE TABLE ".db_maketablename ($table_styles_rel)." (id INT NOT NULL auto_increment, style_id int, template_id INT, identifier VARCHAR (20), linked BOOL, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
    mysql_query ("INSERT INTO ".db_maketablename ($table_styles_rel)." (style_id, template_id, identifier, linked) VALUES ('$style_id', '$templ_id1', 'defaultstyle', '1')");
    mysql_query ("INSERT INTO ".db_maketablename ($table_styles_rel)." (style_id, template_id, identifier, linked) VALUES ('$style_id', '$templ_id2', 'defaultstyle', '0')");
  }
  
  /* Create 'files' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_files)." LIMIT 0,1");
  if (!$check)
  {
    mysql_query ("CREATE TABLE ".db_maketablename ($table_files)." (id INT NOT NULL auto_increment, filename VARCHAR (100), filename_orig VARCHAR (100), name VARCHAR (50), comment TEXT, doctype INT, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
    mysql_query ("INSERT INTO ".db_maketablename ($table_files)." (filename, filename_orig, name, doctype) VALUES ('mirasol_icon.png', 'mirasol_icon.png', 'Mirasol CMS icon', '".array_search ("Images", $doctypes)."')");
    mysql_query ("INSERT INTO ".db_maketablename ($table_files)." (filename, filename_orig, name, doctype) VALUES ('poweredby.png', 'poweredby.png', 'Powered by Mirasol CMS', '".array_search ("Images", $doctypes)."')");
    $file_id = mysql_insert_id ($connection);
  }

  /* Create 'files_rel' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_files_rel)." LIMIT 0,1");
  if (!$check)
  {
    mysql_query ("CREATE TABLE ".db_maketablename ($table_files_rel)." (id INT NOT NULL auto_increment, file_id int, template_id INT, identifier VARCHAR (20), PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
    mysql_query ("INSERT INTO ".db_maketablename ($table_files_rel)." (file_id, template_id, identifier) VALUES ('$file_id', '$templ_id1', 'img_powered')");
    mysql_query ("INSERT INTO ".db_maketablename ($table_files_rel)." (file_id, template_id, identifier) VALUES ('$file_id', '$templ_id2', 'img_powered')");
  }

  db_close ($connection);
}
?>
